//
//  NSString+CustomString.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/15.
//

#import "NSString+CustomString.h"

@implementation NSString (CustomString)

+ (BOOL)isBlankString:(NSString *)string {
    
    if (string == nil || string == NULL) {
    
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        
        return YES;
    }
    
    return NO;
}

//判断是否有表情
+ (BOOL)isContainsEmoji:(NSString *)string {
    __block BOOL isEomji = NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        const unichar hs = [substring characterAtIndex:0];
        // surrogate pair
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    isEomji = YES;
                }
            }
        } else if (substring.length > 1) {
            const unichar ls = [substring characterAtIndex:1];
            if (ls == 0x20e3) {
                isEomji = YES;
            }
        } else {
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
                isEomji = YES;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                isEomji = YES;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                isEomji = YES;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                isEomji = YES;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50|| hs == 0x231a ) {
                isEomji = YES;
            }
        }
    }];
    return isEomji;
}

+ (NSString *)changeChineseString:(NSString *)string {
    
    NSString *newString;
    
    for (int i = 0; i < [string length]; i ++){
        
        int a = [string characterAtIndex:i];
        //有中文
        if( a > 0x4e00 && a < 0x9fff) {
            
            NSCharacterSet *set = [NSCharacterSet URLFragmentAllowedCharacterSet];
            
            newString = [string stringByAddingPercentEncodingWithAllowedCharacters:set];
        }
    }
    
    return newString;
}

+ (NSString *)transfromToPinyin:(NSString *)string {

    //转成了可变字符串
    NSMutableString *str = [NSMutableString stringWithString:string];

    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);

    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);

    NSString *pinYinStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    pinYinStr = [pinYinStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];

    pinYinStr = [pinYinStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    pinYinStr = [pinYinStr stringByReplacingOccurrencesOfString:@" " withString:@""];

    pinYinStr = [pinYinStr lowercaseString];

    return pinYinStr;
}

+ (NSString *)changetoLower:(NSString *)str {
    
    for (NSInteger i = 0; i < str.length; i ++) {
        
        if ([str characterAtIndex:i] >= 'A' && [str characterAtIndex:i] <= 'Z') {
            
            char  temp = [str characterAtIndex:i] + 32;
            NSRange range = NSMakeRange(i, 1);
            str = [str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]];
        }
    }
    return str;
}

+ (NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    if ([format isEqual:@"HH:mm:ss"] && confromTimespStr.length<8) {
        confromTimespStr = @"00:00:00";
    }
    if ([format isEqual:@"YYYY-MM-dd HH:mm:ss"] && confromTimespStr.length<18) {
        confromTimespStr = @"2000-01-01 00:00:00"; //默认返回
    }
    return confromTimespStr;
}



NSString *formatAmount(NSDecimalNumber *amount) {
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
    formatter.numberStyle = NSNumberFormatterDecimalStyle; // 使用千位分隔符
    formatter.groupingSeparator = @",";                    // 分隔符为逗号
    formatter.groupingSize = 3;                            // 每 3 位分隔
    formatter.minimumFractionDigits = 0;                   // 最少保留x位小数
    formatter.maximumFractionDigits = 0;                   // 最多保留x位小数
    
    return [formatter stringFromNumber:amount];
}


+ (void)stringToAttribute:(NSMutableAttributedString *)attributeString font:(UIFont *)font range:(NSRange)range {
    
    [attributeString addAttribute:NSFontAttributeName value:font range:range];
}

+ (void)stringToAttribute:(NSMutableAttributedString *)attributeString color:(UIColor *)color range:(NSRange)range {
    
    [attributeString addAttribute:NSForegroundColorAttributeName value:color range:range];
}


+ (NSString *)toCapitalLetters:(NSString *)money
{
    if (money.length == 0) {
            return @"";
        }
        if (money.floatValue == 0) {
            return @"零圆整";
        }
        //大写数字
        NSArray *upperArray = @[ @"零",@"壹",@"贰",@"叁",@"肆",@"伍",@"陆",@"柒",@"捌",@"玖" ];
        /** 整数部分的单位 */
        NSArray *measureArray = @[ @"", @"拾", @"佰", @"仟"];
        /** 整数部分的单位 */
        NSArray *intUnit = @[@"圆", @"万", @"亿"];
        /** 小数部分的单位 */
        NSArray *floatUnitArray = @[ @"角", @"分" ];
        
        NSString *upIntNum = [NSString string];
        NSString *upFloatNum = [NSString string];
        NSArray *numArray = [money componentsSeparatedByString:@"."];
        
        NSString *str1 = [numArray objectAtIndex:0];
        NSInteger num1 = str1.integerValue;
        for (int i = 0; i < intUnit.count && num1 > 0; i++) {//这一部分就是单纯的转化
            NSString *temp = @"";
            int tempNum = num1%10000;
            if (tempNum != 0 || i == 0) {
                for (int j = 0; j < measureArray.count && num1 > 0; j++) {
                    temp = [NSString stringWithFormat:@"%@%@%@", [upperArray objectAtIndex:num1%10], [measureArray objectAtIndex:j],temp];//每次转化最后一位数
                    num1 = num1/10;//数字除以10
                }
                upIntNum = [[temp stringByAppendingString:[intUnit objectAtIndex:i]] stringByAppendingString:upIntNum];
            } else {
                num1 /= 10000;
                temp = @"零";
                upIntNum = [temp stringByAppendingString:upIntNum];
            }
            
        }
        
        for (int m = 1; m < measureArray.count; m++) { //把零佰零仟这种情况转为零
            NSString *lingUnit = [@"零" stringByAppendingString:[measureArray objectAtIndex:m]];
            upIntNum = [upIntNum stringByReplacingOccurrencesOfString:lingUnit withString:@"零"];
        }
        
        while ([upIntNum rangeOfString:@"零零"].location != NSNotFound) {//多个零相邻的保留一个零
            upIntNum = [upIntNum stringByReplacingOccurrencesOfString:@"零零" withString:@"零"];
        }
        for (int k = 0; k < intUnit.count * 2; k++) { //零万、零亿这种情况转化为万零
            NSString *unit = [intUnit objectAtIndex:k%intUnit.count];
            NSString *lingUnit = [@"零" stringByAppendingString:unit];
            upIntNum = [upIntNum stringByReplacingOccurrencesOfString:lingUnit withString:[unit stringByAppendingString:@"零"]];
        }
        
        if (numArray.count == 2) {//小数部分转化
            NSString *floatStr = [numArray objectAtIndex:1];
            for (NSInteger i = floatStr.length; i > 0; i--) {
                NSString *temp = [floatStr substringWithRange:NSMakeRange(floatStr.length - i, 1)];
                NSInteger tempNum = temp.integerValue;
                if (tempNum == 0) continue;
                NSString *upNum = [upperArray objectAtIndex:tempNum];
                NSString *unit = [floatUnitArray objectAtIndex:floatStr.length - i];
                if (i < floatStr.length && upFloatNum.length == 0 && upIntNum.length > 0) {
                    upFloatNum = @"零";
                }
                upFloatNum = [NSString stringWithFormat:@"%@%@%@", upFloatNum, upNum, unit];
            }
        }
        if (upFloatNum.length == 0) {
            upFloatNum = @"整";
        }
        
        NSString *amountInWords = [NSString stringWithFormat:@"%@%@", upIntNum, upFloatNum];
        
        while ([amountInWords rangeOfString:@"零零"].location != NSNotFound) {//再次除去多余的零
            amountInWords = [amountInWords stringByReplacingOccurrencesOfString:@"零零" withString:@"零"];
        }
        
        if ([amountInWords rangeOfString:@"零整"].location != NSNotFound) {
            amountInWords = [amountInWords stringByReplacingOccurrencesOfString:@"零整" withString:@"整"];
        }
        
        return amountInWords;
}




@end
